package com.gk.study.controller;

import com.gk.study.common.APIResponse;
import com.gk.study.common.ResponeCode;
import com.gk.study.entity.Ad;
import com.gk.study.entity.Ad;
import com.gk.study.permission.Access;
import com.gk.study.permission.AccessLevel;
import com.gk.study.service.AdService;
import com.gk.study.service.AdService;
import com.gk.study.service.FileService;
import com.gk.study.utils.DataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/ad")
public class AdController {

    private final static Logger logger = LoggerFactory.getLogger(AdController.class);

    @Autowired
    AdService service;
    @Autowired
    FileService fileService;
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public APIResponse list(){
        List<Ad> list =  service.getAdList();
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }

    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Transactional
        public APIResponse create(Ad ad) throws IOException {
        if(ad.getImageFile()!=null){
            String fileName= DataUtil.setFileName(ad.getImageFile().getOriginalFilename());
            String path=fileService.path+"/" + fileName;
            ad.getImageFile().transferTo(new File(path));
            ad.setImage(fileService.urlHead+"/"+fileName);
        }
        service.createAd(ad);
        return new APIResponse(ResponeCode.SUCCESS, "创建成功");
    }
//    public APIResponse create(Ad ad,@RequestParam("imageFile") MultipartFile file) throws IOException {
//        if(file!=null){
//            String fileName= DataUtil.setFileName(file.getOriginalFilename());
//            String path=fileService.path+"/" + fileName;
//            file.transferTo(new File(path));
//            ad.setImage(fileService.urlHead+"/"+fileName);
//        }
//        service.createAd(ad);
//        return new APIResponse(ResponeCode.SUCCESS, "创建成功");
//    }

    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public APIResponse delete(String ids){
        System.out.println("ids===" + ids);
        // 批量删除
        String[] arr = ids.split(",");
        for (String id : arr) {
            service.deleteAd(id);
        }
        return new APIResponse(ResponeCode.SUCCESS, "删除成功");
    }

//    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @Transactional
//    public APIResponse update(@RequestBody  Ad ad) throws IOException {
    public APIResponse update(Ad ad) throws IOException {
        if(ad.getImageFile()!=null){
            String fileName= DataUtil.setFileName(ad.getImageFile().getOriginalFilename());
            String path=fileService.path+"/" + fileName;
            ad.getImageFile().transferTo(new File(path));
            ad.setImage(fileService.urlHead+"/"+fileName);
        }
//         if(ad.getImageFile()!=null){
//           String fileName= DataUtil.setFileName(ad.getImageFile().getOriginalFilename());
//           String path=fileService.path+"/" + fileName;
//           ad.getImageFile().transferTo(new File(path));
//           ad.setImage(fileService.urlHead+"/"+fileName);
//       }

        service.updateAd(ad);

        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }

}
